<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Index extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        
        $this->data['controller'] = 'index';         
        $this->load->model('Mindex');
        $this->load->model('Mstatics');
    }
    
    public function index()
    {
        switch($this->session->userdata('level'))
        {
            case 2:
                redirect('report-agency');
                break;
            case 3:
                redirect('report-user');
				break;
			case 4:
                redirect('report-agency-place');
                break;
        }
        
		$this->checkAdmin($this->session->userdata('id_user'));
        $_data = $this->Mindex->getCount($this->session->userdata('id_user'));
        $campaignReport = $this->Mindex->getCampaignReport();
        $this->data['campaignReport'] = $campaignReport['data'];
        
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;

        $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function rejectrequest()
    {
        $this->data['action'] = 'reject';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function place()
    {
        switch($this->session->userdata('level'))
        {
            case 2:
                redirect('place-agency');
                break;
            case 3:
                redirect('place-user');
                break;
            case 4:
                redirect('place-agency-place');
                break;
        }
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->data['place'] = $this->Mindex->getPlace();
        $this->data['action'] = 'place';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function reportplace()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        /*
        $this->data['id'] = $this->getParamInt('id');
        $time_select = $this->getParamString('time_select');
        $data_time = $this->Mstatics->getTimeCampaign($this->data['id'],$this->session->userdata('admin_id'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['type'] = $this->getParamInt('type');
        $this->data['chart_ab'] = $this->Mstatics->getChartAb($this->data['id'], $this->data['time'], $this->data['type'], $this->session->userdata('admin_id'));
        
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = 25;
        $this->data['page'] = $this->getParamUri(2,3);
        $this->data['per_page'] = $config['per_page'];
        if(!in_array($this->data['id'], array(null, '', '0')) || !in_array($this->data['time_select'], array(null, '', '0')))
        {
            $config['paramhack'] = '?id='.$this->data['id'].'&type='.$this->data['type'].'&time='.$this->data['time'].'&time_select='.$this->data['time_select'].'#page';
        }
        
        $offer = (($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0);
        $id_user = $this->session->userdata('admin_id');
        $data = $this->Mstatics->getLogAccess($config['per_page'],$offer,$id_user,$this->data['id'],$this->data['time']);
        
        
        $config['base_url'] = base_url('page_ajax_detail/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $config['ellipsize'] = TRUE;
        $config['stt_reduced']  = 10;
        $config['reduced']  = 10;
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
        
        $_data = $this->Mstatics->getCount($this->session->userdata('admin_id'),$this->data['time'],$this->data['id']);
        //echo "<pre>";die(print_r($_data));
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;
        
        
        //$this->data['data'] = $this->Mcontact->getdetail($id);
        
        $this->data['selectType'] = $this->selectType($this->getParamInt('type'));
        */
        $this->data['action'] = 'reportplace';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function notification()
    {
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->getParamUri(2,3);
        $data = $this->Mindex->getNotificationDB($config['per_page'],(($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0),$this->session->userdata('admin_id'));
        $config['base_url'] = base_url($this->uri->segment(1).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = 'notification';
        $this->load->view('temp/layout', $this->data);
    }
    
    
    public function rnotification()
    {
        $this->checkAdmin($this->session->userdata('id_user'));
        $id = $this->getParamInt('id');
        
        $this->data['data'] = $this->Mindex->getdetailNotification($id, $this->session->userdata('admin_id'));
        
        $this->data['action'] = 'rnotification';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function ajaxgetcampaignreport(){
        $last_id = $this->getParamInt('last_id');
        $campaignReport = $this->Mindex->ajaxGetCampaignReport($last_id);
        die(json_encode($campaignReport['data']));
    }
    
    public function popupreporting()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        $_data = $this->Mindex->getCount($this->session->userdata('id_user'));
        $campaignReport = $this->Mindex->getReportExport();
        $this->data['campaignReport'] = $campaignReport['data'];
        $this->data['listAgency'] = $this->Mindex->getListAgency();
        $this->data['listCustomers'] = $this->Mindex->getListCustomers();
        
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;
        
        $this->data['action'] = 'reportexport';
        $this->load->view('temp/index/reportexport', $this->data);
    }
    
    public function ajaxpopupreporting()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        $_data = $this->Mindex->getCount($this->session->userdata('id_user'));
        
        $times = $this->getParamString('times');
        $times_start = $this->getParamString('times_start');
        $times_end = $this->getParamString('times_end');
        $agency_id = $this->getParamString('agency_id');
        $customer_id = $this->getParamString('customer_id');
        $campaignReport = $this->Mindex->getAjaxReportExport($times,$times_start,$times_end,$agency_id,$customer_id);
        
        $this->data['campaignReport'] = $campaignReport['data'];
        $this->data['listAgency'] = $this->Mindex->getListAgency();
        $this->data['listCustomers'] = $this->Mindex->getListCustomers();
        
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;
        
        $this->data['action'] = 'ajaxreportexport';
        $this->load->view('temp/index/ajaxreportexport', $this->data);
    }
    
    public function ajaxgetcustomers()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        
        $agency_id = $this->getParamInt('agency_id');
        $this->data['data'] = $this->Mindex->ajaxGetListCustomers($agency_id);
        
        $this->data['action'] = 'ajaxgetcustomers';
        $this->load->view('temp/index/ajaxgetcustomers', $this->data);
    }
    
    public function getmodem(){
        $nas_id = $this->getParamString('nas_id');
		$info_ram = $this->getParamString('info_ram');
		$info_cpu = $this->getParamString('info_cpu');
		$info_users = $this->getParamInt('info_users');
        die(json_encode($this->Mindex->getModem($nas_id,$info_ram,$info_cpu,$info_users)));
    }
    	
}

